﻿Module DisplayClient
    Friend WithEvents DS As DisplayServer.DisplayServerClient
    Friend DisplayID As Integer
    Friend PageHost As PageHost
    Friend Host As String
    
    Friend CurrentQuestion As DisplayServer.Question

    Friend GameName As String
    Dim RequestUpdatesTimer As System.Threading.Timer
    Delegate Sub SafeUpdate(ByVal Params As Object)

    Dim LastUpdate As Date = Now

    Sub RequestUpdates()
        If LastUpdate.AddSeconds(15) >= Now Then
            DS.NeedUpdateAsync(DisplayID)
        Else
            DS.HelloAsync()
        End If
    End Sub

    Private Sub DS_GamePageCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles DS.GamePageCompleted
        PageHost.Navigate(New GamePage(GameName))
    End Sub

    Private Sub DS_MainPageCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles DS.MainPageCompleted
        PageHost.Navigate(New MainPage)
    End Sub

    Private Sub DS_OpenCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles DS.OpenCompleted
        DS.HelloAsync()
    End Sub

    Private Sub DS_HelloCompleted(ByVal sender As Object, ByVal e As DisplayServer.HelloCompletedEventArgs) Handles DS.HelloCompleted
        DisplayID = e.Result
        DisplayClient.PageHost.CurrentPage.Dispatcher.BeginInvoke(New SafeUpdate(AddressOf UpdateStatus), "Display ID: " & DisplayID)
        LastUpdate = Now
        If RequestUpdatesTimer Is Nothing Then
            RequestUpdatesTimer = New System.Threading.Timer(New System.Threading.TimerCallback(AddressOf RequestUpdates), "", 100, 500)
        End If
    End Sub

    Private Sub DS_NeedUpdateCompleted(ByVal sender As Object, ByVal e As DisplayServer.NeedUpdateCompletedEventArgs) Handles DS.NeedUpdateCompleted
        If e.Error Is Nothing Then
            LastUpdate = Now
        End If
        Select Case e.Result
            Case DisplayServer.RequiredUpdate.None
            Case DisplayServer.RequiredUpdate.CurrentGames
                DS.GetGamesAsync(DisplayID)
            Case DisplayServer.RequiredUpdate.Status
                DS.GetStatusAsync(DisplayID)
            Case DisplayServer.RequiredUpdate.Scores
                DS.GetScoresAsync(DisplayID)
            Case DisplayServer.RequiredUpdate.SendQuestion
                DS.GetQuestionAsync(DisplayID)
            Case DisplayServer.RequiredUpdate.StartQuestion
                PageHost.CurrentPage.Dispatcher.BeginInvoke(AddressOf CType(PageHost.CurrentPage, GamePage).StartQuestion)
            Case DisplayServer.RequiredUpdate.EndQuestion
                DS.GetCorrectAnswerAsync(DisplayID)
            Case DisplayServer.RequiredUpdate.EndGame
                PageHost.CurrentPage.Dispatcher.BeginInvoke(AddressOf CType(PageHost.CurrentPage, GamePage).EndGame)
        End Select
    End Sub

    Private Sub DS_GetGamesCompleted(ByVal sender As Object, ByVal e As DisplayServer.GetGamesCompletedEventArgs) Handles DS.GetGamesCompleted
        PageHost.CurrentPage.Dispatcher.BeginInvoke(New SafeUpdate(AddressOf UpdateGamesList), e.Result)
    End Sub

    Sub UpdateGamesList(ByVal Result As Dictionary(Of String, Integer))
        Dim SelectedItem As String = ""
        With CType(PageHost.CurrentPage, MainPage).CurrentGamesListBox
            If Not .SelectedIndex = -1 Then SelectedItem = .SelectedItem
            .Items.Clear()
            For Each Game As String In Result.Keys
                .Items.Add(Game)
            Next
            .SelectedItem = SelectedItem
        End With
    End Sub

    Private Sub DS_GetStatusCompleted(ByVal sender As Object, ByVal e As DisplayServer.GetStatusCompletedEventArgs) Handles DS.GetStatusCompleted
        PageHost.CurrentPage.Dispatcher.BeginInvoke(New SafeUpdate(AddressOf UpdateStatus), e.Result)
    End Sub

    Sub UpdateStatus(ByVal Status As String)
        If TypeOf PageHost.CurrentPage Is MainPage Then
            CType(PageHost.CurrentPage, MainPage).Console.Text += Status & vbCr
            CType(PageHost.CurrentPage, MainPage).Console.SelectionLength = 0
            CType(PageHost.CurrentPage, MainPage).Console.SelectionStart = CType(PageHost.CurrentPage, MainPage).Console.Text.Length()
        Else
            CType(PageHost.CurrentPage, GamePage).Console.Text += Status & vbCr
            CType(PageHost.CurrentPage, GamePage).Console.SelectionLength = 0
            CType(PageHost.CurrentPage, GamePage).Console.SelectionStart = CType(PageHost.CurrentPage, GamePage).Console.Text.Length()
        End If
    End Sub

    Private Sub DS_GetScoresCompleted(ByVal sender As Object, ByVal e As DisplayServer.GetScoresCompletedEventArgs) Handles DS.GetScoresCompleted
        If Not e.Result Is Nothing Then
            PageHost.CurrentPage.Dispatcher.BeginInvoke(New SafeUpdate(AddressOf CType(PageHost.CurrentPage, GamePage).UpdateScores), e.Result)
        End If
    End Sub

        Private Sub DS_GetQuestionCompleted(ByVal sender As Object, ByVal e As DisplayServer.GetQuestionCompletedEventArgs) Handles DS.GetQuestionCompleted
        CurrentQuestion = e.Result
        PageHost.CurrentPage.Dispatcher.BeginInvoke(AddressOf CType(PageHost.CurrentPage, GamePage).GetQuestionCompleted)
    End Sub

    Private Sub DS_GetCorrectAnswerCompleted(ByVal sender As Object, ByVal e As DisplayServer.GetCorrectAnswerCompletedEventArgs) Handles DS.GetCorrectAnswerCompleted
        PageHost.CurrentPage.Dispatcher.BeginInvoke(New SafeUpdate(AddressOf CType(PageHost.CurrentPage, GamePage).EndQuestion), e.Result)
    End Sub
End Module
